package jsonParse

import org.json4s.jackson.Serialization

/**
 * ClassName: JsonParseTestg <br/>
 * Description: <br/>
 * date: 2020/8/17 16:25<br/>
 *
 * @author Hesion<br/>
 * @version
 * @since JDK 1.8
 */
object JsonParseTest {

  import org.json4s._
  import org.json4s.jackson.JsonMethods._
  import org.json4s.jackson.Serialization.{read, write}


  def main(args: Array[String]): Unit = {
    val product =
      """
        |{"name":"Tony","price":35.35}
      """.stripMargin
    implicit val formats = Serialization.formats(NoTypeHints)
    //这样的方法不可以直接将对象转成字符串对象
    //    val str: String = compact(render(Product("电视", 10.5)))
    //将对象解析成字符串应该这样谢
    val jsonStr: String = write(Product("ToWrite", 10))
    println(jsonStr)
    //解析为对象
    val obj: Product = parse(product).extract[Product]
    println(obj)
    val obj1: Product = read[Product](product)
    println(obj1)


  }
}

case class Product(name: String, price: Double)